/*
 * AbstractListModel.java  0.4.0 / Oct 24, 2010
 *
 * Copyright (c) 2010 - 2011, cute4j.com and contributors.
 *
 * This software is licensed under the LGPL version 2.1 license.
 * A copy of this license is included in the file LICENSE-LGPL.txt.
 */
package com.cute4j.swing.list;

import com.cute4j.core.events.EventListenerList;
import com.cute4j.swing.events.ListDataEvent;
import com.cute4j.swing.events.ListDataListener;


/**
 * Abstract List Model.
 *
 * @author Slobodan Vrkacevic
 * @version 0.4.0
 */
public abstract class AbstractListModel implements ListModel {

// Protected Fields. -----------------------------------------------------------

  protected final EventListenerList listenerList;

// Public Constructors. --------------------------------------------------------

  /**
   * Constructs new AbstractListModel object.
   */
  public AbstractListModel() {
    listenerList = new EventListenerList();
  }

// Protected Methods. ----------------------------------------------------------

  /**
   * Returns list data listeners.
   *
   * @return list data listeners
   */
  protected ListDataListener[] getListDataListeners() {
    int count = listenerList.getListenerCount(ListDataListener.class);
    ListDataListener[] listeners = new ListDataListener[count];
    if (count > 0) {
      System.arraycopy(listenerList.getListenersArray(ListDataListener.class),
                       0, listeners,  0, count);
    }
    return listeners;
  }

  /**
   * Fires contents changed event.
   *
   * @param source the event source
   * @param index0 the lower index of the range
   * @param index1 the upper index of the range
   */
  protected void fireContentsChanged(Object source, int index0, int index1) {
    int count = listenerList.getListenerCount(ListDataListener.class);
    if (count > 0) {
      ListDataListener[] listeners = getListDataListeners();
      ListDataEvent e = new ListDataEvent(source,
                                          ListDataEvent.CONTENTS_CHANGED,
                                          index0, index1);
      for (int i = count - 1; i >= 0; i--) {
        listeners[i].contentsChanged(e);
      }
    }
  }

  /**
   * Fires interval added event.
   *
   * @param source the event source
   * @param index0 the lower index of the range
   * @param index1 the upper index of the range
   */
  protected void fireIntervalAdded(Object source, int index0, int index1) {
    int count = listenerList.getListenerCount(ListDataListener.class);
    if (count > 0) {
      ListDataListener[] listeners = getListDataListeners();
      ListDataEvent e = new ListDataEvent(source,
                                          ListDataEvent.INTERVAL_ADDED,
                                          index0, index1);
      for (int i = count - 1; i >= 0; i--) {
        listeners[i].intervalAdded(e);
      }
    }
  }

  /**
   * Fires interval removed event.
   *
   * @param source the event source
   * @param index0 the lower index of the range
   * @param index1 the upper index of the range
   */
  protected void fireIntervalRemoved(Object source, int index0, int index1) {
    int count = listenerList.getListenerCount(ListDataListener.class);
    if (count > 0) {
      ListDataListener[] listeners = getListDataListeners();
      ListDataEvent e = new ListDataEvent(source,
                                          ListDataEvent.INTERVAL_REMOVED,
                                          index0, index1);
      for (int i = count - 1; i >= 0; i--) {
        listeners[i].intervalRemoved(e);
      }
    }
  }

// ListModel Interface Implementation. -----------------------------------------

  /**
   * {@inheritDoc}
   */
  public void addListDataListener(ListDataListener l) {
    listenerList.add(ListDataListener.class, l);
  }

  /**
   * {@inheritDoc}
   */
  public void removeListDataListener(ListDataListener l) {
    listenerList.remove(ListDataListener.class, l);
  }

}
